﻿using System;
using System.Configuration;
using System.IO;
using System.Security.Cryptography;
using System.Web.Services;

/// <summary>
/// Summary description for UploadService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class UploadService : WebService
{
    [WebMethod]
    public string Upload(Byte[] buffer, string folderItem, string extention, String itemId)
    {
        var rootPath = Context.Request.PhysicalApplicationPath;
        if (String.IsNullOrEmpty(folderItem)) folderItem = "product";
        var uploadPath = ConfigurationManager.AppSettings["FileServer.Upload"] + folderItem + "\\" +
                         DateTime.Now.ToString("yyyyMMdd") + (itemId != "" ? "\\" + itemId : "");
        var filePath = rootPath + uploadPath;
        if (!Directory.Exists(filePath))
        {
            try
            {
                Directory.CreateDirectory(filePath);
            }
            catch
            {
            }
        }
        try
        {
            var fileName = CreateHash() + extention;
            var fileStream = new FileStream(filePath + "\\" + fileName, FileMode.Create, FileAccess.Write);
            fileStream.Write(buffer, 0, buffer.Length);
            fileStream.Close();
            return uploadPath.Replace("\\", "/") + "/" + fileName;
        }
        catch (Exception)
        {
            return "";
        }
    }

    private static String CreateHash()
    {
        var md5 = MD5.Create();
        var hash = md5.ComputeHash(Guid.NewGuid().ToByteArray());
        return BitConverter.ToString(hash).Replace("-", "");
    }

    [WebMethod]
    public bool DeleteFile(String fileName, string folderItem, String itemId)
    {
        var rootPath = Context.Request.PhysicalApplicationPath;
        if (String.IsNullOrEmpty(folderItem)) folderItem = "news";
        var uploadPath = ConfigurationManager.AppSettings["FileServer.Upload"] + folderItem + "\\" + itemId;
        var filePath = rootPath + uploadPath + "\\" + fileName;
        filePath = filePath.Replace("/", "\\");

        try
        {
            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }
            if (File.Exists(filePath + ".120.120.jpg"))
            {
                File.Delete(filePath + ".120.120.jpg");
            }
            if (File.Exists(filePath + ".130.100.jpg"))
            {
                File.Delete(filePath + ".130.100.jpg");
            }
            if (File.Exists(filePath + ".250.200.jpg"))
            {
                File.Delete(filePath + ".250.200.jpg");
            }
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }
}